package contextualSearch;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OboParser {

	static ArrayList<String> Stopwords;

	public static void main(String[] args) throws IOException {
		termSeparator();
		context();


	}

	static void termSeparator() throws IOException{
		PrintStream ps = new PrintStream("J:\\BC3_IMT_Training\\Parsed Obo.txt");
		FileReader fr = new FileReader(
				"J:\\BC3_IMT_Training\\annotations\\psi-mi25.obo");
		BufferedReader fbr = new BufferedReader(fr);
		String str = fbr.readLine();
		while (str != null) {
			if (str.equals("[Term]")) {
				str = fbr.readLine();
				while (!str.equals("\n") && !str.equals("[Typedef]")
						&& !str.equals("[Term]")) {
					ps.println(str);
					str = fbr.readLine();
				}

			}

			str = fbr.readLine();
		}
		fr.close();
	}

	static void context() throws IOException{
		Stopwords = getStopwords();
		PrintStream ps = new PrintStream("J:\\BC3_IMT_Training\\Contexts.txt");
		FileReader fr = new FileReader(
				"J:\\BC3_IMT_Training\\Parsed Obo.txt");
		BufferedReader fbr = new BufferedReader(fr);
		String str = fbr.readLine();
		while (str != null) {
			if(str.startsWith("id:")){
				ps.println();
				ps.println(str.substring(4));
			}else if(str.startsWith("name:")){
				ps.println(str.substring(6));
			}else if(str.startsWith("def:")){
				StringTokenizer st = new StringTokenizer(str.substring(6));
				while(st.hasMoreTokens()){
					String s = st.nextToken();

					if(isStopWord(Stopwords, s)==false){
						if(s.endsWith(".\"")){
							ps.println(s.substring(0, s.length()-2));
							break;
						}else{
							
							ps.print(s.replaceAll("[^\\x20-\\x7e]", "")+" ");
						}
					}
				}
			}

			str = fbr.readLine();
		}
		
		fr.close();
	}

	static boolean isSpecialCharacter(String s){
		Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(s);
		boolean b = m.find();
		return b;

	}


	public static ArrayList<String> getStopwords() throws IOException{
		FileReader fr = null;
		ArrayList<String> stop_word_list = new ArrayList<String>();
			fr = new FileReader("J://WorkSpace//CPM Final//stopwords.txt");

			BufferedReader fbr=new BufferedReader(fr);

				String stopword = fbr.readLine();
				while(stopword!=null){
					stop_word_list.add(stopword);
					stop_word_list.add(stopword.toUpperCase());
					stop_word_list.add(stopword.substring(0,1).toUpperCase()+stopword.substring(1));
					stopword=fbr.readLine();
				}
				fr.close();
		return stop_word_list;

	}

	public static boolean isStopWord(ArrayList<String> Stopwords, String str)// exclude stop words from
	// dictionary
	{
		return Stopwords.contains(str);

	}




}
